package main;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * Filter class.
 * 
 * @author kolmas
 * 
 */
class ImageMatcher implements FilenameFilter {

	private String[] imgTypes = { "png", "jpg", "gif", "jpeg" };
	private String regex = "(?<=\\.).*$";
	private ImageLoader img;
	private boolean recursive;

	public ImageMatcher(ImageLoader img, boolean recursive) {
		this.img = img;
		this.recursive = recursive;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
	 */
	@Override
	public boolean accept(File file, String fname) {
		img.incFileProccessed();
		Matcher matcher = Pattern.compile(regex).matcher(fname);
		if (matcher.find()) {
			String ext = matcher.group();
			for (String type : imgTypes) {
				if (type.equalsIgnoreCase(ext)) {
					return true;
				}
			}
		}
		if (recursive) {
			String tmp_name = file.getPath() + "/" + fname;
			if (new File(tmp_name).isDirectory()) {
				img.loadImages(tmp_name, recursive);
			}
		}
		return false;
	}

}

/**
 * Loads all images from certain directory.
 * 
 * @author kolmas
 * 
 */
public class ImageLoader {

	private static boolean ready = false;

	private final static String DEFAULT_LOC = "images/";
	private static HashMap<String, BufferedImage> images = new HashMap<String, BufferedImage>();

	private static int totalImg = 0;
	private static int currentImg = 0;
	private int filesProcessed = 0;
	private int filesTotal = 0;

	public void loadImages(boolean recursive) {
		loadImages(DEFAULT_LOC, recursive);
	}

	/**
	 * Loads all images from certain folder.
	 * 
	 * @param folder
	 */
	public void loadImages(String folder, boolean recursive) {
		try {
			URL url = ClassLoader.getSystemResource(DEFAULT_LOC);
			if (url == null)
				return;
			File dir = new File(ClassLoader.getSystemResource(folder).getFile());
			if (dir == null || !dir.isDirectory())
				return;
			File[] files = dir.listFiles(new ImageMatcher(this, recursive));

			filesTotal += dir.listFiles().length;

			// Fillin the hashmap.
			for (File f : files) {
				images.put(f.getName(), ImageIO.read(f));
				// System.out.println(f.getName());
				currentImg++;
			}
			checkIfFinished();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void checkIfFinished() {
		if (filesTotal == filesProcessed) {
			System.out.println("ImageLoader finished loading images! Loaded ("
					+ images.size() + ") images.");
			ready = true;
		}
	}

	public void incFileProccessed() {
		filesProcessed++;
	}

	/**
	 * Get certain image
	 * 
	 * @param fname
	 * @return
	 */
	public static BufferedImage getImage(String fname) {
		if (!isReady())
			return null;
		BufferedImage tmp = images.get(fname);
		if (tmp == null)
			System.err.println("Cant find image: " + fname);
		return tmp;
	}

	/**
	 * Are images loaded?
	 * 
	 * @return
	 */
	public static boolean isReady() {
		return ready;
	}

	public static int getTotal() {
		return totalImg;
	}

	public static int getCurrent() {
		return currentImg;
	}
}
